package com.zhiche.lisa.bms.service.contract.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.contract.ContractFinalpayMapper;
import com.zhiche.lisa.bms.dao.model.contract.Contract;
import com.zhiche.lisa.bms.dao.model.contract.ContractFinalpay;
import com.zhiche.lisa.bms.service.contract.ContractFinalpayService;
import com.zhiche.lisa.bms.service.contract.ContractService;
import com.zhiche.lisa.core.supports.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同油卡明细 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-10-28
 */
@Service
public class ContractFinalpayServiceImpl extends ServiceImpl<ContractFinalpayMapper, ContractFinalpay> implements ContractFinalpayService {
    @Autowired
    private ContractService contractService;

    /**
     * 批量新增
     *
     * @param contractFinalpays
     * @param contractId
     * @return
     */
    @Override
    public List<ContractFinalpay> insertContractFinalpayList(List<ContractFinalpay> contractFinalpays, Long contractId) {
        if (CollectionUtils.isEmpty(contractFinalpays)) {
            return new ArrayList<>();
        }
        contractFinalpays.forEach(contractFinalpay ->
        {
            contractFinalpay.setHeadId(contractId);
        });
        insertBatch(contractFinalpays);
        return contractFinalpays;
    }

    @Override
    public List<ContractFinalpay> selectContractFinalpayListByHeaderId(Long headerId) {
        EntityWrapper<ContractFinalpay> contractFinalpayServiceEntityWrapper = new EntityWrapper<>();
        contractFinalpayServiceEntityWrapper.eq("head_id", headerId);
        return this.selectList(contractFinalpayServiceEntityWrapper);
    }

    @Override
    public List<ContractFinalpay> selectContractFinalpayListByHeadCode(String headerCode) {
        Contract contract = contractService.selectContractByCode(headerCode);
        if (contract == null) {
            throw new BaseException("对应合同不存在");
        }
        return this.selectContractFinalpayListByHeaderId(contract.getId());
    }
}

